package com.zipking.cloud.springbootmybatis.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zipking.cloud.springbootmybatis.persistance.entity.MsgLog;
import com.zipking.cloud.springbootmybatis.service.IMsgLogService;
import com.zipking.cloud.springbootmybatis.service.MultiThreadQueryUtil;
import com.zipking.cloud.springbootmybatis.util.BatchUtils;
import com.zipking.cloud.springbootmybatis.util.CompletableFutureDemo;
import com.zipking.cloud.springbootmybatis.util.ListUtils;
import com.zipking.cloud.springbootmybatis.util.SimpleThreadExecuteUtils;
import com.zipking.cloud.springbootmybatis.util.SpringUtils;
import com.zipking.cloud.springbootmybatis.util.WorkThread;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;
@Slf4j
@RestController
@RequestMapping("/")
public class ThreadController {
    @Autowired
    private ThreadPoolTaskExecutor queryBatchPool;
    @Autowired
    private IMsgLogService msgLogService;


    @GetMapping("/thread1")
    public Object thread1() {
        //msgLogService.asyncBatchInit();
        List<MsgLog> msgLogList= msgLogService.lambdaQuery().last("limit 1000").list();

        SimpleThreadExecuteUtils<MsgLog> threadPoolTaskExecutor =
                new SimpleThreadExecuteUtils<MsgLog>((ArrayList<MsgLog>) msgLogList,  queryBatchPool, 8);
        threadPoolTaskExecutor.Execute(WorkThread .class);
        return  "ok";
    }
    @GetMapping("/thread2")
    public Object thread2() {
        //msgLogService.asyncBatchInit();
        List<MsgLog> msgLogList= msgLogService.logicHandler(new HashMap<>());
        //      List<MsgLog> msgLogList= new ArrayList();
        List<MsgLog> newList=new ArrayList();
        List<List<MsgLog>> partition = ListUtils.averageAssign(msgLogList, 10);
        for(List<MsgLog> p:partition){
            List<MsgLog>  AA= msgLogService.testTime(p,newList);
            log.info("aa:{}",AA.size());
        }


        return  "ok";
    }
    @Autowired
    private MultiThreadQueryUtil multiThreadQueryUtil;
    @GetMapping("/allInfo")
    public Callable<Object> allInfo(HttpServletResponse response) throws Exception {
        CompletableFuture scoreFuture = CompletableFuture
                .supplyAsync(() -> multiThreadQueryUtil.getMultiCombineResult(), queryBatchPool)
                .exceptionally(ex -> Arrays.asList(ex.getStackTrace())) ;
        Callable<Object> cb = () -> {
            CompletableFuture.allOf(scoreFuture).join() ;
            return  "查询成功";
        } ;
     return  cb;
    }


    @GetMapping("/thread3")
    public Object thread3() {



        List<Integer> msgLogList= Lists.newArrayList();
        msgLogList.add(1);
        CompletableFutureDemo demo = new CompletableFutureDemo();
        List  parallelFutureJoin = demo.parallelFutureJoin(msgLogList, s -> {
            MsgLog list = msgLogService.getById(1L);
            log.info("查询成功");
            return list;
        }, (e, num) -> {
            log.error("我异常了，我刚才在处理数字：" + num + "，异常原因：" + e);
            return -1;
        });
        return  "ok";
    }
}
